
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class FileAnalyzer {

	BufferedReader fileReader;
	private String[] paragraphs;
	private String readLine;
	private String mp3Filename;
	long[] transitionInstants;
	
	public FileAnalyzer(String filename) {
		try {
			fileReader = new BufferedReader(new FileReader(filename));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		analyze();
	}

	private void analyze() {
		try {
			computeDates(readLine());
			computeParagraphs();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public String[] getParagraphs() {
		return paragraphs;
	}
	
	private void computeParagraphs() throws Throwable {
		ignoreLineReturns();
		StringBuilder stringBuilder = new StringBuilder();
		char sep = '@';

		while (readLine != null) {
			stringBuilder.append(readLine).append('\n');
			while (((readLine = readLine()) != null)
					&& (!"".equals(readLine.replaceAll("[ \t]", "")))) {
				stringBuilder.append(readLine).append('\n');
			}
			stringBuilder.append(sep);
			ignoreLineReturns();
		}
		paragraphs = stringBuilder.toString().split("@");
	}

	private String readLine() throws Throwable {
		String result = fileReader.readLine();
		return result;
	}

	private void ignoreLineReturns() throws Throwable {
		while (((readLine = readLine()) != null)
				&& ("".equals(readLine.replaceAll("[ \t]", "")))) {
			// do nothing
		}
	}

	private void computeDates(String input) throws Throwable {
		input = input.replace("s", "");
		input = input.replace("m", ",");
		String[] entries = input.split("[ ]");
		SimpleDateFormat dateFormat = new SimpleDateFormat("mm,ss");
		transitionInstants = new long[entries.length];

		try {
			long ref = dateFormat.parse("00,00").getTime();
			for (int i = 0; i < entries.length; i++) {
				transitionInstants[i] = dateFormat.parse(entries[i]).getTime() - ref;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		mp3Filename = readLine();
	}

	public String getMp3Filename() {
		return mp3Filename;
	}

	public long[] getTransitionInstants() {
		return transitionInstants;
	}
}
